package dgut.party.business.mockdata;

import dgut.party.business.dao.rbac.AuthorityRepository;
import dgut.party.business.dao.rbac.RoleRepository;
import dgut.party.business.entity.rbac.Authority;
import dgut.party.business.entity.rbac.Role;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class MockRoleAuthorityBean {
  public static void initAuthority(RoleRepository roleRepo, AuthorityRepository authRepo) {
    Set<Authority> allApis = new HashSet<Authority>();
    allApis.addAll(userApis());
    allApis.addAll(pubApis());
    allApis.addAll(orgApis());
    allApis.addAll(menusAuths());
    allApis.addAll(meApis());
    authRepo.saveAll(allApis);
    roleRepo.save(Role.builder().name("NORMAL_USER").description("门户用户")
      .authorities(allApis.stream()
        .filter((auth) -> !auth.getUrl().startsWith("/mg"))
        .collect(Collectors.toSet()))
      .build());
    roleRepo.save(Role.builder().name("ADMIN").description("管理员用户")
      .authorities(allApis).build());
  }

  public static Set<Authority> menusAuths() {
    Set<Authority> menus = new HashSet<Authority>();
    menus.add(Authority.builder().httpMethod("POST").name("menu_root").url("rootmenu").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_org").url("/org").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_pub").url("/pub").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_sys").url("/sys").build());

    menus.add(Authority.builder().httpMethod("POST").name("menu_org_info").url("/org/info").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_org_orgTree").url("/org/orgTree").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_org_member").url("/org/member").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_org_mg").url("/mg/org").build());

    menus.add(Authority.builder().httpMethod("POST").name("menu_pub_page").url("/pub/page").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_pub_mg_page").url("/mg/pub/page").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_pub_mg_create").url("/mg/pub/create").build());

    menus.add(Authority.builder().httpMethod("POST").name("menu_sys_user_info").url("/sys/userInfo").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_sys_mg_account").url("/mg/sys/account").build());
    menus.add(Authority.builder().httpMethod("POST").name("menu_sys_mg_role_auth_map").url("/mg/sys/roleAuthMap").build());
    return menus;
  }

  public static Set<Authority> orgApis() {
    Set<Authority> apis = new HashSet<Authority>();

    apis.add(Authority.builder().httpMethod("POST").name("org_page").url("/org/page").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_detail").url("/org/detail").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_superior").url("/org/superior").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_subs").url("/org/subs").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_disable").url("/mg/org/disable").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_migrate").url("/mg/org/migrate").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_member_add").url("/mg/org/member/add").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_member_rm").url("/mg/org/member/rm").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_member_assign").url("/mg/org/member/assign").build());

    apis.add(Authority.builder().httpMethod("POST").name("org_create").url("/mg/org/create").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_create").url("/mg/org/create").build());

    apis.add(Authority.builder().httpMethod("POST").name("org_del").url("/mg/org/del").build());
    apis.add(Authority.builder().httpMethod("POST").name("org_edit").url("/mg/org/edit").build());
    return apis;
  }

  public static Set<Authority> pubApis() {
    Set<Authority> apis = new HashSet<Authority>();
    apis.add(Authority.builder().httpMethod("POST").name("pub_page").url("/pub/page").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_detail").url("/pub/detail").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_disable").url("/mg/pub/disable").build());

    apis.add(Authority.builder().httpMethod("POST").name("pub_info_page").url("/pub/info/page").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_act_page").url("/pub/act/page").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_create").url("/mg/pub/create").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_delete").url("/mg/pub/delete").build());
    apis.add(Authority.builder().httpMethod("POST").name("pub_edit").url("/mg/pub/edit").build());
    return apis;
  }

  public static Set<Authority> userApis() {
    Set<Authority> apis = new HashSet<Authority>();


    apis.add(Authority.builder().httpMethod("POST").name("user_page").url("/user/page").build());
    apis.add(Authority.builder().httpMethod("POST").name("user_detail").url("/user/detail").build());
    apis.add(Authority.builder().httpMethod("POST").name("user_create").url("/mg/user/create").build());
    apis.add(Authority.builder().httpMethod("POST").name("user_del").url("/mg/user/del").build());
    apis.add(Authority.builder().httpMethod("POST").name("user_edit").url("/mg/user/edit").build());
    apis.add(Authority.builder().httpMethod("POST").name("user_disable").url("/mg/user/disable").build());
    return apis;
  }

  public static Set<Authority> meApis() {
    Set<Authority> apis = new HashSet<Authority>();

    apis.add(Authority.builder().httpMethod("POST").name("me_brief").url("/me/brief").build());
    apis.add(Authority.builder().httpMethod("POST").name("me_detail").url("/me/detail").build());
    apis.add(Authority.builder().httpMethod("POST").name("me_changepwd").url("/me/changepwd").build());
    apis.add(Authority.builder().httpMethod("POST").name("me_orgs").url("/me/orgs").build());
    apis.add(Authority.builder().httpMethod("POST").name("menu_me").url("/me/menus").build());
    return apis;
  }
}
